package com.telenav.meishi;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class Util {
	private static HashMap<String, String> iconMap = new HashMap<String, String>();

	public static Bitmap getBitmapImage(String dir, String imageName) throws Exception {
		File file = new File("/mnt/sdcard/meishi/food.index");
		if (!file.exists()) {
			throw new Exception("Please put index file to " + file.getAbsolutePath());
		}
		FileInputStream fis = new FileInputStream(file);
		InputStreamReader isr = new InputStreamReader(fis);
		BufferedReader br = new BufferedReader(isr);
		String line;
		String[] lineStrs;
		String t = null;
		while (true) {
			line = br.readLine();
			if (line == null) {
				break;
			}
			lineStrs = line.split("\\|\\|");

			if (lineStrs[0].equals(dir)) {
				t = lineStrs[1];
				break;
			}
		}
		br.close();
		isr.close();
		fis.close();
		if (t != null) {
			file = new File("/mnt/sdcard/meishi/food" + t + ".zip");
			if (!file.exists()) {
				throw new Exception("Please put detail file to " + file.getAbsolutePath());
			}
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			ZipInputStream zis = new ZipInputStream(new FileInputStream(file));
			ZipEntry ze = null;
			String zname;
			while ((ze = zis.getNextEntry()) != null) {
				zname = ze.getName();
				if (zname.equals(dir + File.separator + imageName)) {

					byte[] buffer = new byte[1024];
					int length = 0;

					while ((length = zis.read(buffer)) > 0) {
						baos.write(buffer, 0, length);
					}
					zis.closeEntry();
					break;
				}
				zis.closeEntry();
			}
			zis.close();

			if (baos.toByteArray().length > 0) {
				Bitmap image = BitmapFactory.decodeByteArray(baos.toByteArray(), 0, baos.size());
				return image;
			}
		}
		return null;
	}
	
	public static synchronized HashMap<String, String> getFoodMap() throws Exception{
		if (iconMap.isEmpty()) {
			File file = new File("/mnt/sdcard/meishi/food.index");
			if (!file.exists()) {
				throw new Exception("Please put index file to " + file.getAbsolutePath());
			}

			FileInputStream fis = new FileInputStream(file);
			InputStreamReader isr = new InputStreamReader(fis);
			BufferedReader br = new BufferedReader(isr);
			String line;
			String[] lineStrs;
			while (true) {
				line = br.readLine();
				if (line == null) {
					break;
				}
				lineStrs = line.split("\\|\\|");
				
				iconMap.put(lineStrs[0], lineStrs[1]);
			}
			br.close();
			isr.close();
			fis.close();
			
			file = new File("/mnt/sdcard/meishi/recept.index");
			if (!file.exists()) {
				throw new Exception("Please put index file to " + file.getAbsolutePath());
			}

			fis = new FileInputStream(file);
			isr = new InputStreamReader(fis);
			br = new BufferedReader(isr);
			while (true) {
				line = br.readLine();
				if (line == null) {
					break;
				}
				lineStrs = line.split("\\|\\|");
				
				iconMap.put(lineStrs[0], lineStrs[1]);
			}
			br.close();
			isr.close();
			fis.close();
		}
		return iconMap;
	}

	public static Bitmap[] getBitmapImageIcon(String[] dirs) throws Exception {
		HashMap<String, HashMap<String, Bitmap>> tMap = new HashMap<String, HashMap<String, Bitmap>>();
		
		getFoodMap();
		
		HashMap<String, Bitmap> bitMaps =new HashMap<String, Bitmap>();
		for(String dir:dirs){
			String t = iconMap.get(dir);
			if (tMap.containsKey(t)) {
				HashMap<String, Bitmap> iMap = tMap.get(t);
				iMap.put(dir, null);
				tMap.put(t, iMap);
			} else {
				HashMap<String, Bitmap> iMap = new HashMap<String, Bitmap>();
				iMap.put(dir, null);
				tMap.put(t, iMap);
			}
		}

		
		Iterator<String> it = tMap.keySet().iterator();
		while (it.hasNext()) {
			String t = it.next();
			HashMap<String, Bitmap> iMap = tMap.get(t);
			File file = new File("/mnt/sdcard/meishi/icon" + t + ".zip");
			if (!file.exists()) {
				throw new Exception("Please put detail file to " + file.getAbsolutePath());
			}
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			ZipInputStream zis = new ZipInputStream(new FileInputStream(file));
			ZipEntry ze = null;
			String zname;
			while ((ze = zis.getNextEntry()) != null) {
				zname = ze.getName();
				if (iMap.containsKey(zname.substring(0, zname.length() - 4))) {

					byte[] buffer = new byte[1024];
					int length = 0;

					while ((length = zis.read(buffer)) > 0) {
						baos.write(buffer, 0, length);
					}
					zis.closeEntry();

					Bitmap image = BitmapFactory.decodeByteArray(baos.toByteArray(), 0, baos.size());
					bitMaps.put(zname, image);
				}
				zis.closeEntry();
			}
			zis.close();
		}
		Bitmap[] bitmap = new Bitmap[dirs.length];
		for (int i = 0; i < dirs.length; i++) {
			bitmap[i] = bitMaps.get(dirs[i] + ".jpg");
		}
		return bitmap;
	}
}
